/*! \ingroup modelviews
*/

#ifndef SEQUENCEDIAGAMVIEW_H
#define SEQUENCEDIAGAMVIEW_H

#include <QAbstractTableModel>
#include <QItemDelegate>
#include <QTableView>

#include <dynamicmodel.h>
#include <viewinterface.h>
#include <interfaces.h>

class SequenceDiagramItem
{
public:
    SequenceDiagramItem( ARSExecItem * startItem = 0, ARSExecItem * endItem = 0 );
    ARSExecItem * startItem() const;
    void setStartItem( ARSExecItem * item );
    ARSExecItem * endItem() const;
    void setEndItem( ARSExecItem * item );

private:
    ARSExecItem * m_startItem;
    ARSExecItem * m_endItem;
};

typedef QList<SequenceDiagramItem *> SequenceDiagramItemList;

class SequenceDiagramModel : public QAbstractTableModel
{
    Q_OBJECT

public:
    enum Role
    {
        ItemRole = Qt::UserRole,
        HoverRole
    };

    SequenceDiagramModel( DynamicModel * model );
    int rowCount(const QModelIndex &parent) const;
    int columnCount(const QModelIndex &parent) const;
    QVariant data(const QModelIndex &index, int role) const;
    QVariant headerData(int section, Qt::Orientation orientation, int role) const;
    void setHovered(const QModelIndex & index);
    QModelIndex hovered() const;

private:
    ARSExecItem * nextBlock( ARSExecItem * item );

    ARSExecItemList m_list;
    ARSDummyFormList m_formList;
    QModelIndex m_hovered;
};

///////// SequenceDiagramDelegate  /////////////////////////////////////////////////////////////////

class SequenceDiagramDelegate : public QItemDelegate
{
    Q_OBJECT

public:
    SequenceDiagramDelegate( QObject * parent );
    void paint( QPainter * painter, const QStyleOptionViewItem & option, const QModelIndex & index ) const;

private:
    QImage alImage;
    QImage alImageHover;
    QImage filterImage;
    QImage filterImageHover;
};

class SequenceDiagramView : public QTableView
{
    Q_OBJECT

public:
    static const int sectionWidth = 11;

    SequenceDiagramView( QWidget * parent = 0 );
    void setDynamicModel( DynamicModel * model );
    void paintEvent( QPaintEvent * event );
    void mouseMoveEvent( QMouseEvent * event );
    void mousePressEvent( QMouseEvent *event );

signals:
    void openItem( ARSExecItem * item );

private:
    SequenceDiagramModel * m_model;
};

class SequenceViewWidget : public DynamicViewWidget
{
    Q_OBJECT

public:
    SequenceViewWidget( QWidget * parent = 0 );

    QString pluginName() const;
    void connectToolBarItems(const ToolBarItemList & items);
    void disconnectToolBarItems(const ToolBarItemList & items);
    void adjustToolbar(const ToolBarItemList & itemsList) const;
    void setModel(DynamicModel * model);

signals:
    void openItem( ARSExecItem * item );

private:
    SequenceDiagramView * m_view;
};

class SequenceViewPlugin : public QObject,
                           public ViewPluginInterface
{
    Q_INTERFACES(PluginInterface ViewPluginInterface)
    Q_OBJECT

public:
    SequenceViewPlugin(QObject * parent = 0);

    QString name()const;
    QString description()const;
    bool loadSettings();
    QString viewName()const;
    ViewWidget::ViewType viewType() const;
    QIcon icon(bool active)const;
    ViewWidget * createView(QWidget * parent = 0)const;
    ToolBarItemList toolBarItems(QObject * parent)const;
    SettingsWidget * settingsWidget(QWidget * parent = 0)const;
    void appEvent(AppEvent * event);
};

#endif // SEQUENCEDIAGAMVIEW_H
